import { GaQueryOptions } from './GaQueryOptions';
import relationalStore from '@ohos.data.relationalStore';
import { ConfigurationConstant, Context } from '@kit.AbilityKit';
import { ValuesBucket } from '@kit.ArkData';

export class GaDbConnection {
  rdbStore?: relationalStore.RdbStore
  static context: Context

  static initClass(context: Context) {
    this.context = context;
  }

  async connect(dbName: string): Promise<void> {
    const STORE_CONFIG: relationalStore.StoreConfig = {
      name: dbName,
      securityLevel: relationalStore.SecurityLevel.S1
    }
    this.rdbStore = await relationalStore.getRdbStore(GaDbConnection.context, STORE_CONFIG);
  }

  //查询
  async query(option: GaQueryOptions): Promise<relationalStore.ResultSet> {
    return this.rdbStore?.querySql(option.sql, option.values);
  }

  async insert(tbName: string, valuesBucket: ValuesBucket) {
    return this.rdbStore?.insert(tbName, valuesBucket);
  }

  async execute(option: GaQueryOptions): Promise<void> {
    return this.rdbStore?.executeSql(option.sql, option.values);
  }
}